﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using PlanningGIS.ManageUtility.Modules;
using PlanningGIS.ManageUtility.DataAccess;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using PlanningGIS.ManageUtility;

namespace PlanningGIS.Manager
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DepartmentComboTree : ComboTree
    {
        /// <summary>
        /// 
        /// </summary>
        public DepartmentComboTree()
        {
            InitializeComponent();
            this.DropDownHeight = 200;
            this.DropDownWidth = 200;
        }

        /// <summary>
        /// 
        /// </summary>
        private List<Department> Departments = null;

        /// <summary>
        /// 部门默认的编号
        /// </summary>
        public string DefaultID { get { return "rootnode"; } }

        /// <summary>
        /// 当前选择的部门ID
        /// </summary>
        public string SelectedDepartmentID
        {
            get
            {
                if (this.SelectedNode == null || this.SelectedNode.Tag == null) return "";
                Department d = (Department)this.SelectedNode.Tag;
                return d.ID;
            }
            set
            {
                SetSelectValue(value);
            }
        }

        /// <summary>
        /// 绑定部门数据
        /// </summary>
        public void BindData()
        {
            try
            {
                this.Nodes.Clear();
                Departments = ManageHelper.DBContext.GetAllEntry<Department>();
                if (Departments == null || Departments.Count == 0) return;
                Node n = new Node();
                n.Name = DefaultID;
                n.Text = "";
                n.Tag = null;
                this.Nodes.Add(n);
                AddNode("", null);
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 设置当前选择的部门
        /// </summary>
        /// <param name="departmentid"></param>
        public void SetSelectValue(string departmentid)
        {
            if (string.IsNullOrWhiteSpace(departmentid)) departmentid = DefaultID;
            Node[] nd = this.Nodes.Find(departmentid, true);
            if (nd != null && nd.Length > 0) this.SelectedNode = nd[0];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentid"></param>
        /// <param name="parentnode"></param>
        private void AddNode(string parentid, Node parentnode)
        {
            List<Department> nds = Departments.FindAll(t => parentid.Equals(t.Parent_ID, StringComparison.CurrentCultureIgnoreCase));
            if (nds == null || nds.Count == 0) return;
            foreach (Department d in nds)
            {
                Node n = new Node();
                n.Name = d.ID;
                n.Text = d.Name;
                n.Tag = d;
                AddNode(d.ID, n);
                if (parentnode == null)
                {
                    this.Nodes.Add(n);
                }
                else
                {
                    parentnode.Nodes.Add(n);
                }
            }
        }
    }
}
